package com.szz.cardecision.controller;

import com.szz.cardecision.entity.User;
import com.szz.cardecision.mapper.UserMapper;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.Optional;

@Controller
public class HomeController {
    private final UserMapper userMapper;

    public HomeController(UserMapper userMapper) {
        this.userMapper = userMapper;
    }

    @GetMapping("/")
    public String index() {
        return "index";
    }

    @GetMapping("/dashboard")
    public String dashboard(Authentication auth, Model model) {
        Optional<User> user = userMapper.findByUsername(auth.getName());
        user.ifPresent(u -> model.addAttribute("user", u));
        return "dashboard";
    }
}